package com.jxzn.locust.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jxzn.locust.dao.AbstractRepository;
import com.jxzn.locust.dao.RoleResourceRepository;
import com.jxzn.locust.model.Role;
import com.jxzn.locust.model.RoleResource;
import com.jxzn.locust.service.RoleResourceService;

@Service
@Transactional
public class RoleResourceServiceImpl implements RoleResourceService {

    @Autowired
    RoleResourceRepository roleResourceRepository;

    @Override
    public AbstractRepository<RoleResource> getRepository() {
        return roleResourceRepository;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<RoleResource> saveAll(List<RoleResource> tList) {
        int roleId = tList.get(0).getRoleId();
        roleResourceRepository.deleteByRoleId(roleId);
        if (!tList.isEmpty()) {
            return RoleResourceService.super.saveAll(tList);
        } else {
            return tList;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveAll(Role role, List<RoleResource> rrList) {
        roleResourceRepository.deleteByRoleId(role.getId());
        if (!rrList.isEmpty()) {
            RoleResourceService.super.saveAll(rrList);
        }
    }

}
